package org.jeecg.modules.quartz.job;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.stock.entity.TbStockMinuteCountVo;
import org.jeecg.modules.demo.stock.entity.TbStockMinuteVo;
import org.jeecg.modules.demo.stock.service.ITbStockMinuteService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 示例带参定时任务
 *
 * @Author Scott
 */
@Slf4j
public class SampleParamJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }


    //股票分钟交易
    @Autowired
    private ITbStockMinuteService tbStockMinuteService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" Job Execution key：" + jobExecutionContext.getJobDetail().getKey());
        log.info(String.format("welcome %s! Jeecg-Boot 带参数定时任务 SampleParamJob !   时间:" + DateUtils.now(), this.parameter));
        String code = this.parameter;
        if (StringUtils.isNoneBlank(code)){
        getList(code);
        }
    }


    public void getList(String str) {
        String[] arr = str.split(",");
        for(String code:arr){
            List<TbStockMinuteVo> list = tbStockMinuteService.selectList(code,"desc");
            int count= 0;
            for (TbStockMinuteVo vo : list) {
                count++;
                String timeId = vo.getTimeId();
                Double value1 = vo.getValue1();
                String dayTime = vo.getDayTime();
                String va = "时间:"+dayTime+" 价格:"+value1;
                boolean test = test(count);
                if (test){
                    System.out.println(timeId);
                    System.out.println(va);
                }else{
                    System.out.println(va);
                }
            }
            //根据时间排序
            try {
                timeSort(list);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            System.out.println("==========================");

        }
        System.out.println("结束");
    }

    //根据时间排序
    private void timeSort(List<TbStockMinuteVo> list) throws ParseException {
//        sortClass sort = new sortClass();
//        Collections.sort(list,sort);
//        for(int i = 0; i< list.size(); i++){
//            TbStockMinuteVo temp = (TbStockMinuteVo) list.get(i);
//            System.out.println("时间:"+temp.getDayTime()+",价格:"+temp.getValue1());
//        }
//        String s = JSONObject.toJSONString(list);
//        System.out.println(s);
        /**
         * 根据时间段分类
         * 9:30 - 9:59
         * 10:00-10:29
         * 10:30-10.59
         * 11:00-11.30
         * 11:30
         * 1:00-1:29
         * 1:30-1:59
         * 2:00-2:29
         * 2:30-3:00
         * 3:00
         * 4个小时
         */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date now = null;
        int number1 = 0;
        int number2 = 0;
        int number3 = 0;
        int number4 = 0;

        int number5 = 0;
        int number6 = 0;
        int number7 = 0;
        int number8 = 0;
List<TbStockMinuteCountVo> countVoList = new ArrayList<>();


        for (TbStockMinuteVo tbStockMinuteVo : list) {
            String dayTime = tbStockMinuteVo.getDayTime();
            now = formatter.parse(dayTime);
            int hours = now.getHours();//小时
            int minutes = now.getMinutes();//分钟
//            System.out.println(hours + " " + minutes);
            if (hours == 9) {
                number1++;
            }else if (hours == 10 && minutes <= 29){
                number2++;
            }else if (hours == 10 && minutes >= 30){
                number3++;
            }else if (hours == 11){
                number4++;
            }else if (hours == 13 && minutes <= 29){
                number5++;
            }else if (hours == 13 && minutes >= 30){
                number6++;
            }else if (hours == 14 && minutes <= 29){
                number7++;
            }else if (hours == 14 && minutes >= 30 || hours == 15){
                number8++;
            }else{
                System.out.println(tbStockMinuteVo);
            }
        }
        DecimalFormat df=new DecimalFormat("0.00");
        int sum = list.size();

        //数量统计
        TbStockMinuteCountVo countVo1 = new TbStockMinuteCountVo( "9:30",number1,df.format((float) number1 / sum),9);
        TbStockMinuteCountVo countVo2 = new TbStockMinuteCountVo("10:00",number2,df.format((float) number2 / sum),8);
        TbStockMinuteCountVo countVo3 = new TbStockMinuteCountVo("10:30",number3,df.format((float) number3 / sum),7);
        TbStockMinuteCountVo countVo4 = new TbStockMinuteCountVo("11:00",number4,df.format((float) number4 / sum),6);
        TbStockMinuteCountVo countVo5 = new TbStockMinuteCountVo("13:00",number5,df.format((float) number5 / sum),5);
        TbStockMinuteCountVo countVo6 = new TbStockMinuteCountVo("13:30",number6,df.format((float) number6 / sum),4);
        TbStockMinuteCountVo countVo7 = new TbStockMinuteCountVo("14:00",number7,df.format((float) number7 / sum),3);
        TbStockMinuteCountVo countVo8 = new TbStockMinuteCountVo("14:30",number8,df.format((float) number8 / sum),2);
        countVoList.add(countVo1);
        countVoList.add(countVo2);
        countVoList.add(countVo3);
        countVoList.add(countVo4);
        countVoList.add(countVo5);
        countVoList.add(countVo6);
        countVoList.add(countVo7);
        countVoList.add(countVo8);

        System.out.println("总共:"+list.size());
        String s1 = JSONObject.toJSONString(countVoList);
        System.out.println(s1);


    }




    public boolean test(int x){
        if (x%2==0) {
//            System.out.println("x是偶数呢");
            return false;

        } else {
//            System.out.println("x是奇数呢~");
            return  true;
        }

    }
}
